package com.yt.dor.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.dor.models.dto.DormRoomDTO;
import com.yt.dor.models.service.IDormRoomService;
import com.yt.dor.models.vo.DormBuildingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.yt.dor.models.service.IDormBuildingService;
import com.yt.dor.models.bo.DormBuildingSaveBO;
import com.yt.dor.models.bo.DormBuildingBO;
import com.yt.dor.models.dto.DormBuildingDTO;

/**
 * 宿舍楼表控制器
 *
 * @author thekai
 * @Date 2025-10-21 14:12:50
 */

@RestController
@RequestMapping("/cms/dormBuilding")
@Slf4j
@Api(value = "DormBuildingController",tags = "宿舍楼表接口")
public class DormBuildingController {

    @DubboReference
    private IDormBuildingService dormBuildingService;
    @DubboReference
    private IDormRoomService dormRoomService;

    @ApiOperation(value = "添加宿舍楼表",notes = "添加宿舍楼表")
    @PostMapping(value = "/add")
    public Result insert(@RequestBody DormBuildingSaveBO saveBO) {
        boolean flag = dormBuildingService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改宿舍楼表",notes = "修改宿舍楼表")
    @PostMapping("/update")
    public Result update(@RequestBody DormBuildingSaveBO saveBO){
        DormBuildingDTO dto = new DormBuildingDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = dormBuildingService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }
    @ApiOperation(value = "逻辑删除宿舍楼表",notes = "逻辑删除宿舍楼表")
    @ApiImplicitParam(name = "id",value = "宿舍楼表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        Wrapper wp=new EntityWrapper();
        wp.eq("id",id);
        DormBuildingDTO dto=dormBuildingService.selectOne(wp);
        dto.setIsDel(1);
        boolean flag = dormBuildingService.updateById(dto);
        if (flag){
            return new Result<>("删除成功");
        }
        return new Result(CodeEnum.FAIL_100009);
    }
    @PostMapping("/queryDormBuild")
    @ApiOperation(value = "查询楼栋信息", notes = "查询楼栋信息")
    public Result<List<DormBuildingVO>> queryDormBuild() {
        List<DormBuildingVO> dormBuildingVOS = dormBuildingService.queryDormBuild();
        return new Result<>(dormBuildingVOS);
    }

}
